'use strict';

const Controller = require('egg').Controller;

class NewsController extends Controller {

  async tolist() {
    const { ctx } = this;
    await ctx.render('index.html');
  }

  async totips() {
    const { ctx } = this;
    await ctx.render('tips.html');
  }

  async towelcome() {
    const { ctx } = this;
    await ctx.render('welcome.html');
  }

  /* 异步请求 */
  async list() {
    const { ctx } = this;
    // const { page = 1, limit = 10 } = ctx.query;
    const { page, limit } = ctx.query;
    const obj = await ctx.service.news.list(page, limit);
    const num = await ctx.service.news.count();
    const result = {
      code: 0,
      msg: '',
      count: num,
      data: obj,
    };
    this.ctx.body = result;
  }

  async getOne() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const obj = await ctx.service.news.getNewsById(id);
    this.ctx.body = obj;
  }

  async del() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const obj = await ctx.service.news.delNewsById(id);
    this.ctx.body = obj;
  }

  async save() {
    const { ctx } = this;
    const { id, content, content1, content2 } = ctx.request.body;
    const obj = await ctx.service.news.save(id, content, content1, content2);
    this.ctx.body = obj;
  }
}

module.exports = NewsController;
